package com.sita.bluetactivity;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Jason on 2016/9/8.
 */
public class DeviceAdapter extends BaseAdapter {
    private Context context;
    private List<BluetoothDevice> data = new ArrayList<>();

    public DeviceAdapter(Context context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MyViewHolder holder = null;
        if (convertView==null){
            holder = new MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_layoyt,null);
            holder.names = (TextView) convertView.findViewById(R.id.name);
            holder.addresss = (TextView) convertView.findViewById(R.id.address);
            convertView.setTag(holder);
        }else {
            holder = (MyViewHolder) convertView.getTag();
        }
        //获取对应的蓝牙设备
        BluetoothDevice device = (BluetoothDevice) getItem(position);
        //显示名字
        if (device.getName()!=null){
            holder.names.setText(device.getName());
        }
        if (device.getAddress()!=""){
            holder.addresss.setText(device.getAddress().toString());
        }
        //显示地址

        return convertView;
    }
   public void refresh(List<BluetoothDevice> mdata){
       data = mdata;
       notifyDataSetChanged();
   }
    class MyViewHolder{
        TextView names,addresss;
    }
}
